<template>
  <div class="attention-search mtop">
    <m-header title="关注"></m-header>
    <div class="searchbar">
      <input type="text" v-model.trim="nickname" placeholder="搜索会员账号">
      <span class="btn" @click="handleSearch(true)" v-if="!nickname">搜索</span>
      <span class="btn" @click="handleSearch(false)" v-else>取消</span>
    </div>
    <div class="attention" :class="{'noloading': !attentionList.length}">
      <scroller :on-refresh="refresh" :on-infinite="infinite">
        <div class="attention-list" v-if="attentionList.length"
        :class="{'loaded-data': attentionList.length < 5, 'loadmore-data': attentionList.length < 10}" >
          <div class="atten-item" v-for="(item, index) in attentionList" :key="index">
            <div class="atteninfo" @click="$router.push({ path: `/follow/user/${item.member_id}` })">
              <img class="avatar" :src="item.avatar" alt="">
              <div class="atten-info">
                <div class="title">{{item.nickname}}</div>
                <div class="desc">关注 {{item.follow_num}} | 粉丝 {{item.fans_num}}</div>
                <div class="bouns">累计奖金<span class="money">{{item.bonus_total_price}}</span>元</div>
              </div>
            </div>
            <div class="atten-btn">
              <div class="btn" :class="{on: item.is_follow}" @click="handleAttention(item)">{{item.is_follow?'已关注':'关注'}}</div>
            </div>
          </div>
        </div>
      </scroller>
      <div class="no-datas" v-if="!loading && !attentionList.length">
        <img src="../../assets/imgs/icon-noattention@2x.png" alt="">
        <div class="desc">暂无数据</div>
      </div>
    </div>

  </div>
</template>
<script>
import { mapActions } from 'vuex';

const delay = (function () {
  let timer = 0;
  return function (callback, ms) {
    clearTimeout(timer);
    timer = setTimeout(callback, ms);
  };
})();

export default {
  data () {
    return {
      nickname: '',
      pageNum: 1,
      isPull: false, // 下拉
      loading: true,
      attentionList: [
        // {
        //   member_id: '1',
        //   nickname: '嘻嘻啊1',
        //   avatar: 'https://www.try255.com/statics/avatar/1_160.png',
        //   mobile: '13067870772',
        //   follow_num: '1',
        //   fans_num: '1',
        //   bonus_total_price: '4.00'
        //  // is_follow: true
        // }
        // {
        //   member_id: '2',
        //   avatar: 'http://img0.imgtn.bdimg.com/it/u=1001413611,1569666981&fm=26&gp=0.jpg',
        //   nickname: '巴菲特',
        //   follow_num: 10,
        //   fans_num: 10,
        //   bonus_total_price: 100.00,
        //   is_follow: true
        // }, {
        //   member_id: '3',
        //   avatar: 'http://img2.touxiang.cn/file/20150513/c10267cfad438d8188172d0ccfe27d41.jpg',
        //   nickname: '烈阳在天',
        //   follow_num: 20,
        //   fans_num: 20,
        //   bonus_total_price: 200.00,
        //   is_follow: true
        // }, {
        //   member_id: '4',
        //   avatar: 'http://img2.touxiang.cn/file/20150513/c10267cfad438d8188172d0ccfe27d41.jpg',
        //   nickname: '没把握不下',
        //   follow_num: 30,
        //   fans_num: 30,
        //   bonus_total_price: 300.00,
        //   is_follow: true
        // }, {
        //   member_id: '5',
        //   avatar: 'http://img2.touxiang.cn/file/20150513/c10267cfad438d8188172d0ccfe27d41.jpg',
        //   nickname: '竞彩医疗兵',
        //   follow_num: 40,
        //   fans_num: 40,
        //   bonus_total_price: 400.00,
        //   is_follow: true
        // }
      ]
    }
  },
  methods: {
    ...mapActions({
      memberSearch: 'attention/memberSearch',
      getFollow: 'attention/getFollow',
      getUnFollow: 'attention/getUnFollow'
    }),
    handleSearch (state) {
      if (state) {
        if (!this.nickname) {
          this.$vux.toast.text('请输入会员账号');
        }
      } else {
        this.loading = true,
        this.nickname = '';
        this.attentionList = [];
      }
    },
    refresh (done) { // 下拉
      this.isPull = true;
      this.pageNum = 1;
      this.loading = true;
      this.getDatas(done);
    },
    infinite (done) { // 上滑
      if (!this.isPull && this.nickname) {
        this.loading = true;
        this.getDatas(done);
      } else {
        done(true);
      }
    },
    getDatas (done) {
      if (this.loading) {
        const params = {
          nickname: this.nickname,
          page: this.pageNum,
          size: 10
        }
        this.memberSearch(params).then(res => {
          this.loading = false;
          let listData = res.list || [];
          if (listData.length) {
            if (this.isPull) {
              this.isPull = false;
              this.attentionList = [];
            }
            this.pageNum += 1;
            const listDatas = this.attentionList;
            this.attentionList = listDatas.concat(listData);
            // this.fansList.push(...res.list);
            if (this.pageNum < res.page.total) {
              done(true);
            } else {
              done(2); ;
            }
          } else {
            done(2);
          }
        })
      }
    },
    handleAttention (item) {
      // item.is_follow = !item.is_follow;
      if (item.is_follow) {
        this.getUnFollow({follower_id: item.member_id}).then(res => {
          item.is_follow = 0;
          console.log('res1', res, item);
        })
      } else {
        this.getFollow({follower_id: item.member_id}).then(res => {
          item.is_follow = 1;
          console.log('res2', res, item);
        })
      }
    }
  },
  watch: {
    nickname () {
      delay(() => {
        this.loading = true;
        this.pageNum = 1;
        this.attentionList = [];
        if (this.nickname) {
          this.getDatas(() => {});
        }
      }, 300);
    }
  }
}
</script>
<style lang="less" scoped>
  @import '../../assets/styles/mixins.less';
  .attention-search {
    .searchbar {
      position: relative;
      height: 1.08rem;
      padding: 0.2rem 0.84rem 0.2rem 0.2rem;
      background: #fff;
      box-sizing: border-box;
      &::after {
        content: '';
        display: block;
        width: 100%;
        height: 0.16rem;
        position: absolute;
        left: 0;
        bottom: -0.16rem;
        background: #f7f7f7;
      }
      input {
        display: flex;
        align-content: center;
        height: 100%;
        width: 100%;
        padding: 0.1rem 0.2rem;
        background: #F7F7F7;
        border-radius: 0.17rem;
        box-sizing: border-box;
        &::-webkit-input-placeholder {
          color: #d2d2d2;
          font-size: 0.28rem;
        }
      }
      .btn {
        display: flex;
        align-items: center;
        position: absolute;
        right: 0;
        top: 0;
        height: 100%;
        width: 0.84rem;
        padding-left: 0.1rem;
        font-size: 0.28rem;
        color: #333333;
        box-sizing: border-box;
      }
    }
    .attention {
      position: absolute;
      width: 100%;
      top: 2.16rem;
      bottom: 0;
      background: #f7f7f7;
      overflow-y: scroll;
      // background: #ffffff;
      &.noloading {
        top: 2rem;
        // background: rgba(0, 0, 0, 0.4);
      }
      .attention-list {
        margin-top: 0.12rem;
        background: red;
        padding-top: 0.4rem;
        .atten-item {
          display: flex;
          position: relative;
          background: #ffffff;
          &:last-child::after {
            display: none;
          }
          .border-1px-bottom(#d2d2d2);
          .atteninfo {
            display: flex;
            flex: 1;
            padding-top: 0.3rem;
          }
          .avatar {
            display: block;
            margin-left: 0.2rem;
            width: 1rem;
            height: 1rem;
            border-radius: 50%;
          }
          .atten-info {
            position: relative;
            flex: 1;
            margin-left: 0.2rem;
            padding-bottom: 0.2rem;
            // padding-right: 1.33rem;
            overflow: hidden;
            .title {
              font-size: 0.28rem;
              color: #2b2b2b;
              line-height: 0.36rem;
            }
            .desc {
              font-size: 0.24rem;
              color: #b9b9b9;
              line-height: 0.36rem;
            }
            .bouns {
              font-size: 0.24rem;
              color: #b9b9b9;
              line-height: 0.36rem;
              .money {
                color: #d44743;
              }
            }
          }
          .atten-btn {
            display: flex;
            // justify-content: flex-start;
            align-items: center;
            width: 1.33rem;
            .btn {
              // position: absolute;
              // right: 0.2rem;
              // top: 50%;
              // margin-top: -0.25rem;
              width: 1.13rem;
              height: 0.5rem;
              line-height: 0.5rem;
              font-size: 0.24rem;
              color: #ffffff;
              text-align: center;
              background-color: #d44743;
              border: 0.02rem solid #d44743;
              border-radius: 0.06rem;
              box-sizing: border-box;
              &.on {
                color: #d44743;
                background-color: #fff;
              }
            }
          }
          &:last-child {
            .atten-info::after {
              display: none;
            }
          }
        }
      }
      .no-datas {
        position: fixed;
        width: 100%;
        top: 2.16rem;
        bottom: 0;
        height: 100%;
        text-align: center;
        background: #ffffff;
        &::after {
          content: '';
          display: block;
          width: 100%;
          height: 0.16rem;
          position: absolute;
          left: 0;
          top: -0.16rem;
          background: #f7f7f7;
        }
        img {
          display: block;
          width: 4.15rem;
          height: 3.22rem;
          margin: 0 auto;
          padding-top: 0.8rem;
        }
        .desc {
          margin-top: 0.3rem;
          font-size: 0.22rem;
          color: #919191;
        }
      }
    }
  }
</style>
